<?php

namespace common\models\system;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%admin_log}}".
 *
 * @property int $id ID
 * @property int $uid 用户uid
 * @property string $title 标题
 * @property string $controller 控制器
 * @property string $action 动作
 * @property string $querystring 查询字符串
 * @property string $remark 备注
 * @property string $ip IP
 * @property int $created_at 创建时间
 * @property int $status 状态
 */
class AdminLog extends \yii\db\ActiveRecord
{
    // 状态
    const STATUS_正常 = 1;
    const STATUS_隐藏 = -1;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%admin_log}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => null,
                'value' => time(),
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'title', 'controller', 'action', 'querystring', 'remark'], 'required'],
            [['uid', 'created_at', 'status'], 'integer'],
            [['title'], 'string', 'max' => 100],
            [['controller', 'action'], 'string', 'max' => 50],
            [['querystring', 'remark'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => '用户uid',
            'title' => '操作',
            'controller' => '控制器',
            'action' => '动作',
            'querystring' => '查询字符串',
            'remark' => '备注',
            'ip' => 'IP',
            'created_at' => '创建时间',
            'status' => '状态',
        ];
    }

    /**
     * 状态字典
     * @param null $index
     * @return array|mixed
     */
    public static function getStatus($index = null) {
        $map = [
            self::STATUS_正常 => '正常',
            self::STATUS_隐藏 => '隐藏',
        ];

        return ($index === null) ? $map : (isset($map[$index]) ? $map[$index] : '未知');
    }
}
